/**
 * 
 * @module description:计算超标元素的个数&计算需要治理的元素
 * @author 吴迪嘉
 */

const AllProjectDataModel = require('../../models/map/projectInfo3.js')
const AllGroundDataModel = require('../../models/map/groundInfo2.js')
const ReferenceDataMOdel = require('../../models/map/reference.js')
const ElementDataModel = require('../../models/map/elementInfo.js')
const PointInfoModel = require('../../models/map/pointInfo.js')
const sample_lab_ground_info_model = require('../../models/map/sample_detector_ground_info.js')
const common_model = require('../../models/common/Map.js')

var reference, element_Map

common_model.then(data => {
  reference = data.reference_17_ground_Map
  element_Map = data.element_Map
})
class Compute_Controller {



  static async ComputeCount1(ctx) {

    let res = await sample_lab_ground_info_model.getAlldata()
    let res2 = res.map(item => {
      return item = item.dataValues
    })
    var dic = new Array(); //定义一个字典

    for (let x of res2) {
      var count = 0
      for (let i in x) {
        if (x[i] && reference.get(i) && (x[i] > reference.get(i))) {
          count += 1
        }
      }
      const res22 = await sample_lab_ground_info_model.UpdateCount(x.point_num, x.sample_num, x.assess_type,count)
      // const res222 = await PointInfoModel.UpdatedifferentCount(x.point_num, count)
      dic[x.point_num]=0
    }//这个过程结束后sample_lab_ground_info_model每个样本的count都会重新更新一遍
    console.log(dic)

    for (let x of res2) {
      var count = 0
      for (let i in x) {
        if (x[i] && reference.get(i) && (x[i] > reference.get(i))) {
          count += 1
        }
      }
      const res22 = await sample_lab_ground_info_model.UpdateCount(x.point_num, x.sample_num, x.assess_type,count)
      // const res222 = await PointInfoModel.UpdatedifferentCount(x.point_num, count)
      dic[x.point_num]=dic[x.point_num]+count
    }
    console.log(dic)
    for(let i in dic){
      const res22 = await PointInfoModel.UpdatedifferentCount(i,dic[i])

    }
    ctx.body = {
      success: true,
      res: ctx.request.body,
      msg: '获取成功'
    }
  }
    // 计算所有的simple_count, detail_count,fixed_count,all_count

  /**
   *ComputeCount
   *计算超标元素的个数
   * @static
   * @param {*} ctx
   * @memberof Compute_Controller
   */
  static async ComputeCount(ctx) {

    let res = await sample_lab_ground_info_model.getAlldata()//得到所有的数据
    let res2 = res.map(item => {
      return item = item.dataValues
    })
    for (let x of res2) {
      var count = 0
      for (let i in x) {
        if (x[i] && reference.get(i) && (x[i] > reference.get(i))) {
          count += 1
        }
      }

      const res11 = await sample_lab_ground_info_model.UpdateCount(x.point_num, x.sample_num, x.assess_type,count)

    }//这个过程结束后sample_lab_ground_info_model每个样本的count都会重新更新一遍
    // 接下来来更新project_info的count数据
    const res3 = await AllProjectDataModel.getAllProjectData()

    let res4 = res3.map(item => {
      return item = item.dataValues
    })
    for (let item of res4) {
      let Point_numList = await PointInfoModel.getPointnumByProjectnum(item.project_num)

      var simple_count = 0
      var detail_count = 0
      var fixed_count = 0

      for (let x of Point_numList) {

        let data = await sample_lab_ground_info_model.getDataByPointnum(x)

        let res5 = data.map(item => {
          return item = item.dataValues
        })
        //res5里是个列表，列表里放的数据都是同一个point_num
        var point_count = 0
        for (let y of res5) {
          if(y.count>0){
          if (y.assess_type == 1 && y.project_num == item.project_num) {
            
            simple_count += 1
            point_count +=1
          }
          else if (y.assess_type == 2 && y.project_num == item.project_num) {
            detail_count += 1
            point_count += 1

          }
          else if (y.assess_type == 3 && y.project_num == item.project_num) {
            fixed_count += 1
            point_count += 1

          }
        }
        }
        var all_count = simple_count + detail_count + fixed_count
        const res = await AllProjectDataModel.UpdatedifferentCount(item.project_num, simple_count, detail_count, fixed_count, all_count)
         const res10 = await PointInfoModel.UpdatedifferentCount(x, point_count)

      }
    }

  }



  /**
   *ComputeAttention
   *计算需要治理的元素
   * @static
   * @param {*} ctx
   * @memberof Compute_Controller
   */
  static async ComputeAttention(ctx) {

    let Alldata = await sample_lab_ground_info_model.getAlldata()
    let res = Alldata.map(item => {
      return item = item.dataValues
    })
    for (let i of res) {
      var chazhi = 0
      var attention = []
      for (let item in i) {

        if ((i[item] > reference.get(item)) && (i[item] - reference.get(item) > 0) && element_Map.get(item)) {
          chazhi = i[item] - reference.get(item)
          attention.push(item)
        }

      }
      if (chazhi == 0) {
        var attention = []
      }

      var temp_attention = attention.join(",")
      let data = await sample_lab_ground_info_model.UpdateAttention(i.point_num,i.sample_num, temp_attention)

    }

  }
}
module.exports = Compute_Controller
